package com.sinoglobal.shakeprize.activity.prize;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sinoglobal.shakeprize.R;
import com.sinoglobal.shakeprize.activity.address.PrizeAddress;
import com.sinoglobal.shakeprize.activity.base.AbstractActivity;
import com.sinoglobal.shakeprize.bean.PrizeDetails1;
import com.sinoglobal.shakeprize.bean.PrizeDetails2;
import com.sinoglobal.shakeprize.utils.Contstent;
import com.sinoglobal.shakeprize.utils.FuzzyDateTimeFormatter;
import com.sinoglobal.shakeprize.utils.OKhttpUtil;
import com.sinoglobal.shakeprize.utils.OKhttpUtil.ResponseCallback;
import com.sinoglobal.shakeprize.utils.SPUtil;
import com.squareup.picasso.Picasso;

import okhttp3.Request;

/**
 * @author temulu
 * @createdate 2015-8-26下午2:38:36
 * @Description 奖品详情界面
 */
public class MyPrizeDetailsActivity extends AbstractActivity implements
		OnClickListener {
	private static final int DEATLS_1 = 1;
	private static final int DEATLS_2 = 2;
	private static final int DEATLS_FAIL = 3;

	private TextView prizeDetailsStatus, prizeName, prizePrise,
			prizeUserfulLifeTitle, prizeUsefulLife, prizeUserfulTitle,
			prizeusefulinfo, prizeAddressTitle, prizeUserAddress,
			prizeUserName, prizeUserNumber, prizeLogisticsTitle,
			prizeLogisticsCompany, prizeCourierNumber;
	private ImageView prizeImg;
	private RelativeLayout prizeLogisticsInfo;
	private LinearLayout prizeButton;
	private Button prizeHomeDeliveryService, prizeUserByOneself;
	private int userprizeId, pickstatus, accepttype;
	private int prizeId;
	private int roundid;
	private PrizeDetails1 details1;
	private PrizeDetails2 details2;
	private AlertDialog.Builder builder;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.prize_details_activity);
		accepttype = getIntent().getIntExtra("accepttype", 0);
		userprizeId = getIntent().getIntExtra("userprizeId", 0);
		pickstatus = getIntent().getIntExtra("pickstatus", 0);
		prizeId = getIntent().getIntExtra("prizeId", 0);
		roundid = getIntent().getIntExtra("roundid", 0);
		requestPrizeInfo();
		initView();

	}

	private void initView() {
		titleView.setText("奖品详情");
		prizeDetailsStatus = (TextView) findViewById(R.id.prizeDetailsStatus);
		prizeImg = (ImageView) findViewById(R.id.prizeImg);
		prizeName = (TextView) findViewById(R.id.prizeName);
		prizePrise = (TextView) findViewById(R.id.prizePrise);
		prizeUserfulLifeTitle = (TextView) findViewById(R.id.prizeUserfulLifeTitle);
		prizeUsefulLife = (TextView) findViewById(R.id.prizeUsefulLife);
		prizeUserfulTitle = (TextView) findViewById(R.id.prizeUserfulTitle);
		prizeusefulinfo = (TextView) findViewById(R.id.prizeusefulinfo);
		prizeAddressTitle = (TextView) findViewById(R.id.prizeAddressTitle);
		prizeUserAddress = (TextView) findViewById(R.id.prizeUserAddress);
		prizeUserName = (TextView) findViewById(R.id.prizeUserName);
		prizeUserNumber = (TextView) findViewById(R.id.prizeUserNumber);
		prizeLogisticsTitle = (TextView) findViewById(R.id.prizeLogisticsTitle);
		prizeLogisticsInfo = (RelativeLayout) findViewById(R.id.prizeLogisticsInfo);
		prizeLogisticsCompany = (TextView) findViewById(R.id.prizeLogisticsCompany);
		prizeCourierNumber = (TextView) findViewById(R.id.prizeCourierNumber);
		prizeButton = (LinearLayout) findViewById(R.id.prizeButton);
		prizeHomeDeliveryService = (Button) findViewById(R.id.prizeHomeDeliveryService);
		prizeHomeDeliveryService.setOnClickListener(this);
		prizeUserByOneself = (Button) findViewById(R.id.prizeUserByOneself);
		prizeUserByOneself.setOnClickListener(this);
		// 奖品状态：1 积分 已领取(终结状态) ,2 实物 未领取 .3 实物 已过期(终结状态) ,4 实物 待发货, 5 实物 已发货 ,6
		// 实物 完成(终结状态) 7, 实物 自提
		String status = "";
		switch (pickstatus) {
		case 2:
			status = "未领取";
			break;
		case 4:
			status = "待发货";
			prizeUserfulLifeTitle.setVisibility(View.GONE);
			prizeUsefulLife.setVisibility(View.GONE);
			prizeAddressTitle.setVisibility(View.GONE);
			prizeUserAddress.setVisibility(View.GONE);
			prizeUserName.setVisibility(View.GONE);
			prizeUserNumber.setVisibility(View.GONE);
			prizeButton.setVisibility(View.GONE);
			break;
		case 5:
			status = "已发货";
			prizeUserfulLifeTitle.setVisibility(View.GONE);
			prizeUsefulLife.setVisibility(View.GONE);
			prizeAddressTitle.setVisibility(View.GONE);
			prizeUserAddress.setVisibility(View.GONE);
			prizeUserName.setVisibility(View.GONE);
			prizeUserNumber.setVisibility(View.GONE);
			prizeButton.setVisibility(View.GONE);
			prizeLogisticsTitle.setVisibility(View.VISIBLE);
			prizeLogisticsInfo.setVisibility(View.VISIBLE);
			break;
		case 6:
			status = "完成";
			prizeButton.setVisibility(View.GONE);
			Log.e("accepttype", accepttype + "==" + pickstatus);
			switch (accepttype) {
			case 2:// 发货
				prizeUserfulLifeTitle.setVisibility(View.GONE);
				prizeUsefulLife.setVisibility(View.GONE);
				prizeAddressTitle.setVisibility(View.GONE);
				prizeUserAddress.setVisibility(View.GONE);
				prizeUserName.setVisibility(View.GONE);
				prizeUserNumber.setVisibility(View.GONE);
				prizeLogisticsTitle.setVisibility(View.VISIBLE);
				prizeLogisticsInfo.setVisibility(View.VISIBLE);
				break;
			}
			break;
		case 7:
			status = "自提";
			prizeButton.setVisibility(View.GONE);
			break;
		}
		prizeDetailsStatus.setText(status);
	}

	/**
	 * 获取奖品详情
	 */
	private void requestPrizeInfo() {
		Map<String, Object> request1 = new HashMap<String, Object>();
		request1.put("userprizeId", userprizeId);
		request1.put("userId", SPUtil.getString(mActivity, Contstent.USER_ID));
		request1.put("prizeId", prizeId);
		String json1 = JSONObject.toJSONString(request1);
		String url1 = Contstent.PRIZE_PATH + "prize/prizeDetailByUId.do?json="
				+ json1;
		OKhttpUtil.enqueue(url1, new ResponseCallback() {

			@Override
			public void onFailure(Request arg0, IOException arg1) {

			}

			@Override
			public void onResponse(String json) throws IOException {
				if (!TextUtils.isEmpty(json)) {
					Log.e("url1", json);
					try {
						details1 = JSON.parseObject(json, PrizeDetails1.class);
					} catch (Exception e) {
						Toast.makeText(MyPrizeDetailsActivity.this, "访问网络失败，请稍后再试", Toast.LENGTH_SHORT).show();
						return;
					}
					setData1();
				}
			}
		});
		Map<String, Object> request2 = new HashMap<String, Object>();
		request2.put("roundid", "" + roundid);
		String json2 = JSONObject.toJSONString(request2);
		String url2 = Contstent.SHAKE_PATH + "yaoyiyao/roundDetail.do?json="
				+ json2;
		OKhttpUtil.enqueue(url2, new ResponseCallback() {

			@Override
			public void onFailure(Request arg0, IOException arg1) {

			}

			@Override
			public void onResponse(String json) throws IOException {
				if (!TextUtils.isEmpty(json)) {
					Log.e("url2", json);
					details2 = JSON.parseObject(json, PrizeDetails2.class);
					setData2();
				}
			}
		});
	}

	/**
	 * 添加数据1
	 */
	private void setData1() {
		Picasso.with(MyPrizeDetailsActivity.this).load(Contstent.PICTURE_PATH + details1.rs.prizepic).into(prizeImg);
		prizeName.setText(details1.rs.name);
		prizePrise.setText("价值： ¥" + details1.rs.price);
		prizeusefulinfo.setText(details1.rs.remark);
		switch (pickstatus) {
		case 5:// 已发货
			Log.e("details1.rs.logisticscompany", details1.rs.logisticscompany);
			prizeLogisticsCompany.setText(details1.rs.logisticscompany);
			prizeCourierNumber.setText(details1.rs.logisticssn);
			break;
		case 6:// 完成
			switch (accepttype) {
			case 2:// 发货
				prizeLogisticsCompany.setText(details1.rs.logisticscompany);
				prizeCourierNumber.setText(details1.rs.logisticssn);
				break;
			}
			break;
		}
	}

	/**
	 * 添加数据2
	 */
	private void setData2() {
		switch (pickstatus) {
		case 2:// 未领取
			prizeUsefulLife.setText(FuzzyDateTimeFormatter
					.time2Chanese(details2.rs.acceptstartdate)
					+ "-"
					+ FuzzyDateTimeFormatter
							.time2Chanese(details2.rs.acceptenddate));
			prizeUserAddress.setText(details2.rs.pickupaddress);
			prizeUserName.setText(details2.rs.pickuplinkmen);
			prizeUserNumber.setText(details2.rs.pickupphone);
			if (0 == details2.rs.isdelivery) {// 送货上门
				prizeHomeDeliveryService.setClickable(false);
				prizeHomeDeliveryService.setBackgroundColor(Color.GRAY);
			}
			if (0 == details2.rs.ispickup) {// 自提0否1是
				prizeUserByOneself.setClickable(false);
				prizeUserByOneself.setBackgroundColor(Color.GRAY);
			}
			break;
		case 6:// 完成
			switch (accepttype) {
			case 1:// 自提
				prizeUsefulLife.setText(FuzzyDateTimeFormatter
						.time2Chanese(details2.rs.acceptstartdate)
						+ "-"
						+ FuzzyDateTimeFormatter
								.time2Chanese(details2.rs.acceptenddate));
				prizeUserAddress.setText(details2.rs.pickupaddress);
				prizeUserName.setText(details2.rs.pickuplinkmen);
				prizeUserNumber.setText(details2.rs.pickupphone);
				break;
			}
			break;
		case 7:// 自提
			prizeUsefulLife.setText(FuzzyDateTimeFormatter
					.time2Chanese(details2.rs.acceptstartdate)
					+ "-"
					+ FuzzyDateTimeFormatter
							.time2Chanese(details2.rs.acceptenddate));
			prizeUserAddress.setText(details2.rs.pickupaddress);
			prizeUserName.setText(details2.rs.pickuplinkmen);
			prizeUserNumber.setText(details2.rs.pickupphone);
			break;

		}
	}

	@Override
	public void onClick(View v) {
		if (details1 != null && details2 != null) {
			final Intent intent = new Intent(this, MyPrizeGetTheWay.class);
			final Bundle bundle = new Bundle();
			bundle.putInt("userprizeId", userprizeId);
			bundle.putString("name", details1.rs.name);
			bundle.putString("prizepic", details1.rs.prizepic);
			Log.e("prizepic", "" + details1.rs.prizepic);
			bundle.putString("price", "价值： ¥" + details1.rs.price);
			bundle.putString("remark", details1.rs.remark);
			bundle.putString(
					"acceptenddate",
					FuzzyDateTimeFormatter
							.time2Chanese(details2.rs.acceptstartdate)
							+ "-"
							+ FuzzyDateTimeFormatter
									.time2Chanese(details2.rs.acceptenddate));
			bundle.putString("price", "价值： ¥" + details1.rs.price);
			if (v.getId()==R.id.prizeHomeDeliveryService) {
				intent.setClass(this, PrizeAddress.class);
				bundle.putInt("mode", 2);
				intent.putExtras(bundle);
				if (builder == null) {
					builder = new AlertDialog.Builder(mActivity);
					builder.setMessage("选择送货上门时，运费由获奖用户承担，是否继续？");
				}
				builder.setPositiveButton("继续",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								startActivity(intent);
								dialog.cancel();
							}
						});
				builder.setNegativeButton("再想想",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.cancel();
							}
						});
				builder.create().show();
			} else if(v.getId()==R.id.prizeUserByOneself){
				intent.setClass(this, MyPrizeGetTheWay.class);
				bundle.putInt("mode", 1);
				intent.putExtras(bundle);
				startActivity(intent);
			}
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

}
